package camid.distribution.activeobject;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class LocalInvocationHandler implements InvocationHandler {

	private ObjectScheduler scheduler;
	
	public LocalInvocationHandler(ObjectScheduler scheduler) 
	{
		this.scheduler = scheduler;
	}

	@Override
	public Object invoke(Object callee, Method method, Object[] params)
			throws Throwable 
	{
		Object result = null;
		try 
		{
			scheduler.acquire();
			result = scheduler.submitTask(method.getName(), params);
		}
		catch(Exception e)
		{	
			throw e;
		}
		finally
		{
			scheduler.release();
		}
		
		return result;
	}

}
